using HKB.Common;
using System;
using System.Data;
using System.Web;
namespace HKB.Web
{
	public class MessageBll
	{
		private Conn conn = null;
		private Pagination _pageHand = null;
		protected Pagination pageHand
		{
			get
			{
				Pagination pageHand;
				if (this._pageHand == null)
				{
					this._pageHand = new Pagination();
					this._pageHand.SQL_CloseTablePre = false;
					this._pageHand.P_Object = Language.GetAdminMsg("Message/MessageName");
					pageHand = this._pageHand;
				}
				else
				{
					pageHand = this._pageHand;
				}
				return pageHand;
			}
		}
		public MessageBll()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public MessageBll(Conn _conn)
		{
			this.conn = _conn;
		}
		public DataTable GetMessages()
		{
			return this.conn.GetDataTable("SELECT * FROM HL_Message Order By MessageID desc");
		}
		public DataTable GetMessages(int indexpage, int maxperpage, string wherestr, string orderbystr)
		{
			return this.pageHand.GetDataTable("HL_Message", indexpage, maxperpage, "MessageID", wherestr, orderbystr, this.conn, null);
		}
		public DataTable GetMessages(int indexpage, int maxperpage, string wherestr, string orderbystr, params IDbDataParameter[] param)
		{
			return this.pageHand.GetDataTable("HL_Message", indexpage, maxperpage, "MessageID", wherestr, orderbystr, this.conn, param);
		}
		public string GetPagePAnel(string linkhead)
		{
			this.pageHand.P_LinkHead = linkhead;
			return this.pageHand.ShowPagePanel();
		}
		public DataTable GetMessageByMessageID(int ID, bool SetReaded)
		{
			return this.conn.GetDataTable(string.Concat(new object[]
			{
				"UPDATE [HL_Message] SET [IsRead] =1 WHERE [MessageID] =",
				ID,
				";SELECT * FROM HL_Message Where MessageID=",
				ID
			}));
		}
		public DataTable GetMessageByMessageID(int ID)
		{
			return this.conn.GetDataTable("SELECT * FROM HL_Message Where MessageID=" + ID);
		}
		public bool AddMessage(string Sender, string Receiver, string Title, string Content, int DelR, int DelS, int IsSend, int IsRead, string SendName, string SendGroup, int IsSave)
		{
			bool result;
			if (Receiver == "Users")
			{
				if (SendName.Contains(","))
				{
					string[] array = SendName.Split(new char[]
					{
						','
					});
					for (int i = 0; i < array.Length; i++)
					{
						this.InsertMessage(Sender, array[i], Title, Content, 0, 0, 1, 0, IsSave);
					}
				}
				else
				{
					this.InsertMessage(Sender, SendName, Title, Content, 0, 0, 1, 0, IsSave);
				}
				result = true;
			}
			else
			{
				if (Receiver == "All")
				{
					UserBll userBll = new UserBll(this.conn);
					DataTable dataTable = userBll.GetUserNameID();
					for (int j = 0; j < dataTable.Rows.Count; j++)
					{
						this.InsertMessage(Sender, dataTable.Rows[j]["UserName"].ToString(), Title, Content, 0, 0, 1, 0, IsSave);
					}
				}
				else
				{
					DataTable dataTable;
					if (SendGroup.Contains(","))
					{
						dataTable = this.conn.GetDataTable("SELECT UserName From [HL_User] Where GroupID in(" + SendGroup + ")");
					}
					else
					{
						dataTable = this.conn.GetDataTable("SELECT UserName From [HL_User] Where GroupID=" + SendGroup);
					}
					for (int j = 0; j < dataTable.Rows.Count; j++)
					{
						this.InsertMessage(Sender, dataTable.Rows[j]["UserName"].ToString(), Title, Content, 0, 0, 1, 0, IsSave);
					}
				}
				result = true;
			}
			return result;
		}
		public bool DeleteMessage(int MessageID)
		{
			int num = this.conn.Execute("DELETE From [HL_Message] Where MessageID=" + MessageID);
			return num >= 1;
		}
		public bool DeleteMessage(string MessageID)
		{
			int num;
			if (MessageID.Contains(","))
			{
				num = this.conn.Execute("DELETE From [HL_Message] Where MessageID in(" + MessageID + ")");
			}
			else
			{
				num = this.conn.Execute("DELETE From [HL_Message] Where MessageID=" + MessageID);
			}
			return num >= 1;
		}
		public bool InsertMessage(string Sender, string Receiver, string Title, string Contents, int DelR, int Dels, int IsSend, int IsRead, int isSave)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@Sender", DbType.String, Sender),
				this.conn.CreateParameter("@Receiver", DbType.String, Receiver),
				this.conn.CreateParameter("@Title", DbType.String, Title),
				this.conn.CreateParameter("@Content", DbType.String, Contents),
				this.conn.CreateParameter("@DelR", DbType.Int32, DelR),
				this.conn.CreateParameter("@DelS", DbType.Int32, Dels),
				this.conn.CreateParameter("@IsSend", DbType.Int32, IsSend),
				this.conn.CreateParameter("@IsRead", DbType.Int32, IsRead),
				this.conn.CreateParameter("@IsSave", DbType.Int32, isSave)
			};
			int num = this.conn.Execute("INSERT INTO [HL_Message] ([Sender], [Receiver], [Title], [Content], [DelR], [SendTime], [DelS], [IsSend], [IsRead],[IsSave]) VALUES (@Sender, @Receiver, @Title, @Content, @DelR, '" + DateTime.Now.ToString() + "', @DelS, @IsSend, @IsRead,@IsSave)", parameters);
			this.conn.Execute("Update [HL_User] Set UnreadMsg=UnreadMsg+1 Where UserName=@username", new IDbDataParameter[]
			{
				this.conn.CreateParameter("@username", DbType.String, Receiver)
			});
			return num == 1;
		}
		public DataTable GetSenderMessage(string Sender)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@Sender", DbType.String, Sender);
			return this.conn.GetDataTable("SELECT * From [HL_Message] Where ([Sender] = @Sender) and DelS=0 and IsSave=1 Order By MessageID Desc", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public DataTable GetReceiverMessage(string Receiver)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@Receiver", DbType.String, Receiver);
			return this.conn.GetDataTable("SELECT * From [HL_Message] Where ([Receiver] = @Receiver) and DelR=0 Order By MessageID Desc", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public bool SetMessageDelR(int MessageID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@MessageID", DbType.Int32, MessageID);
			DataTable dataTable = this.conn.GetDataTable("Select DelS From [HL_Message] WHERE ([MessageID] = " + MessageID + ")");
			bool result;
			if (dataTable.Rows[0]["DelS"].ToString() == "1")
			{
				result = this.DeleteMessage(MessageID);
			}
			else
			{
				result = (this.conn.Execute("UPDATE [HL_Message] SET [DelR] =1 WHERE ([MessageID] = @MessageID)", new IDbDataParameter[]
				{
					dbDataParameter
				}) >= 1);
			}
			return result;
		}
		public bool SetMessageDelS(int MessageID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@MessageID", DbType.Int32, MessageID);
			DataTable dataTable = this.conn.GetDataTable("Select DelR From [HL_Message] WHERE ([MessageID] = " + MessageID + ")");
			bool result;
			if (dataTable.Rows[0]["DelR"].ToString() == "1")
			{
				result = this.DeleteMessage(MessageID);
			}
			else
			{
				result = (this.conn.Execute("UPDATE [HL_Message] SET [DelS] =1 WHERE (([MessageID] = @MessageID))", new IDbDataParameter[]
				{
					dbDataParameter
				}) >= 1);
			}
			return result;
		}
		public bool SetMessageIsRead(int MessageID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@MessageID", DbType.Int32, MessageID);
			return this.conn.Execute("UPDATE [HL_Message] SET [IsRead] =1 WHERE (([MessageID] = @MessageID))", new IDbDataParameter[]
			{
				dbDataParameter
			}) >= 1;
		}
		public int GetUnReadNumByUserName(string UserName)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@UserName", DbType.String, UserName);
			return this.conn.ExecuteCount("SELECT count(MessageID) From [HL_Message] WHERE IsRead=0 and (([Receiver] = @UserName))", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
	}
}
